package org.tlipoca.smsteamstore.service.impl;


import jakarta.annotation.Resource;
import org.genntii.smdomain.entity.SteamAppDetail;
import org.genntii.smdomain.entity.SteamStore;
import org.genntii.smdomain.vo.SteamStoreVO;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.tlipoca.smsteamstore.mapper.SteamStoreMapper;
import org.tlipoca.smsteamstore.service.SteamStoreCacheService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SteamStoreCacheServiceImpl implements SteamStoreCacheService {

    @Resource
    private SteamStoreMapper steamStoreMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    @CachePut(cacheNames = "recommend", key = "'recommend'")
    public ArrayList<SteamAppDetail> recommendApp() {

        ArrayList<Integer> appRecommend = new ArrayList<>();
        appRecommend.add(1172470);
        appRecommend.add(730);
        appRecommend.add(271590);
        appRecommend.add(594650);
        appRecommend.add(230410);
        appRecommend.add(1144400);
        appRecommend.add(412830);
        appRecommend.add(1668940);
        appRecommend.add(2194530);
        appRecommend.add(1364780);
        ArrayList<SteamAppDetail> steamAppDetails = new ArrayList<>();
        for (Integer i : appRecommend){
            SteamAppDetail steamAppDetail = mongoTemplate.findOne(new Query(Criteria.where("steam_appid").is(i)), SteamAppDetail.class, "SteamAppDetail");
            steamAppDetails.add(steamAppDetail);
        }
        return steamAppDetails;
    }

    @Override
    @CacheEvict(cacheNames = "recommend", key = "'recommend'")
    public void clearCache() {

    }

    @Override
    @Cacheable(cacheNames = "recommend", key = "'recommend'")
    public ArrayList<SteamAppDetail> getRecommend() {
        return new ArrayList<>();
    }

    @Override
    @CachePut(cacheNames = "recommendHome", key = "'recommendHome'")
    public ArrayList<SteamStoreVO> setRecommend() {
        ArrayList<Integer> appRecommend = new ArrayList<>();
        appRecommend.add(1172470);
        appRecommend.add(730);
        appRecommend.add(271590);
        appRecommend.add(594650);
        appRecommend.add(230410);
        appRecommend.add(1144400);
        appRecommend.add(412830);
        appRecommend.add(1668940);
        appRecommend.add(2194530);
        appRecommend.add(1364780);

        List<SteamAppDetail> steamAppDetails = mongoTemplate.find(new Query(new Criteria("steam_appid").in(appRecommend)),SteamAppDetail.class,"SteamAppDetail");
        Map<Integer, SteamAppDetail> steamAppDetailMap = new HashMap<>();
        for (SteamAppDetail steamAppDetail:steamAppDetails){
            steamAppDetailMap.put(steamAppDetail.getSteam_appid(),steamAppDetail);
        }
        ArrayList<SteamStore> steamStores = steamStoreMapper.selectListByAppid(appRecommend);
        Map<Integer, SteamStore> steamStoreMap = new HashMap<>();
        for (SteamStore steamStore: steamStores){
            steamStoreMap.put(steamStore.getAppid(),steamStore);
        }
        ArrayList<SteamStoreVO> steamStoreVOS = new ArrayList<>();
        for (Integer i:appRecommend){
            if (i == 55119) continue;
            steamStoreVOS.add(SteamStoreVO.builder()
                    .id(steamStoreMap.get(i).getId())
                    .appid(i)
                    .name(steamStoreMap.get(i).getName())
                    .headerImage(steamAppDetailMap.get(i).getHeader_image())
                    .developers(steamAppDetailMap.get(i).getDevelopers())
                    .tags(steamAppDetailMap.get(i).getCategories())
                    .InitialPrice(steamAppDetailMap.get(i).getPrice_overview() == null?0:steamAppDetailMap.get(i).getPrice_overview().getInitial())
                    .currentPrice(steamAppDetailMap.get(i).getPrice_overview() == null?0:steamAppDetailMap.get(i).getPrice_overview().getInitial())
                    .shortDescription(steamAppDetailMap.get(i).getShort_description())
                    .build());
        }
        return steamStoreVOS;
    }

    @Override
    @Cacheable(cacheNames = "recommendHome", key = "'recommendHome'")
    public ArrayList<SteamStoreVO> getRecommendHome() {
        return new ArrayList<>();
    }
}
